package com.powernode.driver.service.impl;


import com.powernode.driver.config.TencentProperties;
import com.powernode.driver.service.CiService;
import com.powernode.model.vo.order.TextAuditingVo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ciModel.auditing.*;
import com.qcloud.cos.region.Region;
import jakarta.annotation.Resource;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CiServiceImpl implements CiService {

    @Resource
    private TencentProperties tencentProperties;


    private COSClient getCOSClient() {
        ClientConfig clientConfig = new ClientConfig(new Region(tencentProperties.getRegion()));
        BasicCOSCredentials credentials = new BasicCOSCredentials(tencentProperties.getSecretId(), tencentProperties.getSecretKey());

        clientConfig.setHttpProtocol(HttpProtocol.https);

        COSClient cosClient = new COSClient(credentials, clientConfig);

        return cosClient;
    }


    /**
     * 图片审核
     *
     * @param path
     * @return
     */
    @Override
    public Boolean imageAuditing(String path) {
        COSClient cosClient = getCOSClient();

        //图片审核的请求对象
        ImageAuditingRequest request = new ImageAuditingRequest();
        //设置存储桶名称
        request.setBucketName(tencentProperties.getBucketPrivate());
        //设置图片的位置
        request.setObjectKey(path);

        //发送请获取响应
        ImageAuditingResponse response = cosClient.imageAuditing(request);

        cosClient.shutdown();


        //用于返回该审核场景的审核结果，返回值：0：正常。1：确认为当前场景的违规内容。2：疑似为当前场景的违规内容。
        if (!response.getPornInfo().getHitFlag().equals("0")
                || !response.getAdsInfo().getHitFlag().equals("0")
                || !response.getTerroristInfo().getHitFlag().equals("0")
                || !response.getPoliticsInfo().getHitFlag().equals("0")
        ) {
            return false;
        }
        return true;
    }


    /**
     * 审核文本
     */
    @Override
    public TextAuditingVo textAuditingVo(String content) {
        if (!StringUtils.hasText(content)) {
            //没有说话
            TextAuditingVo textAuditingVo = new TextAuditingVo();
            textAuditingVo.setResult("0");

            return textAuditingVo;
        }

        COSClient cosClient = getCOSClient();

        //构建文本请求对象
        TextAuditingRequest request = new TextAuditingRequest();
        request.setBucketName(tencentProperties.getBucketPrivate());

        //将内容转成base64
        byte[] bytes = Base64.encodeBase64(content.getBytes());
        String contentBase64 = new String(bytes);

        request.getInput().setContent(contentBase64);

        //全部场景
        request.getConf().setDetectType("all");

        TextAuditingResponse response = cosClient.createAuditingTextJobs(request);

        AuditingJobsDetail detail = response.getJobsDetail();

        TextAuditingVo textAuditingVo = new TextAuditingVo();

        if ("Success".equals(detail.getState())) {
            //审核完毕
            String result = detail.getResult();

            StringBuffer keywords = new StringBuffer();

            //获取违规关键词集合
            List<SectionInfo> sectionList = detail.getSectionList();

            for (SectionInfo sectionInfo : sectionList) {
                String pornInfoKeyWord = sectionInfo.getPornInfo().getKeywords();
                String illegalInfoKeyWord = sectionInfo.getIllegalInfo().getKeywords();
                if (pornInfoKeyWord.length() > 0) {
                    keywords.append(pornInfoKeyWord).append(",");
                }
                if (illegalInfoKeyWord.length() > 0){
                    keywords.append(illegalInfoKeyWord).append(",");
                }
            }
            textAuditingVo.setResult(result);
            textAuditingVo.setKeywords(keywords.toString());
        }

        return textAuditingVo;
    }
}
